package org.seedltd.upms.controller.detection;

import java.io.IOException;
import java.util.List;

import org.seedltd.base.annotation.IgnoreResponseWrap;
import org.seedltd.base.model.ResponseResult;
import org.seedltd.core.base.BaseController;
import org.seedltd.core.base.BasePage;
import org.seedltd.core.utils.Assert;
import org.seedltd.core.validation.Save;
import org.seedltd.log.annotation.SysLog;
import org.seedltd.upms.model.detection.form.DetectionProductForm;
import org.seedltd.upms.model.detection.form.DetectionProductSearchForm;
import org.seedltd.upms.model.detection.vo.DetectionProductVO;
import org.seedltd.upms.model.sys.vo.SysAttachmentVO;
import org.seedltd.upms.service.detection.DetectionProductService;
import org.seedltd.upms.service.sys.SysAttachmentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import lombok.AllArgsConstructor;

/**
 *
 * 产品管理控制器
 * 
 * @author laizuan
 * @version 1.0
 * @since 2021/07/28
 */
@RestController
@AllArgsConstructor
@RequestMapping("/detectionProduct")
public class DetectionProductController extends BaseController<DetectionProductService> {
    private final SysAttachmentService sysAttachmentService;

    /**
     * 新增
     *
     * @param form
     *            产品管理表单数据
     * @return 产品管理主键
     */
    @SysLog("新增产品")
    @PostMapping(value = "/add")
    @PreAuthorize("hasAuthority('detection:product:add')")
    public Long saveOrUpdate(@Validated({Save.class}) DetectionProductForm form, MultipartFile file)
        throws IOException {
        return baseService.saveOrUpdate(form, file);
    }

    /**
     * 获取详情
     * 
     * @param id
     *            产品管理主键
     * @return 产品管理
     */
    @GetMapping(value = "/get")
    public DetectionProductVO get(Long id) {
        Assert.notNull(id, "参数不能为空");
        DetectionProductVO productVO = baseService.findById(id);
        List<SysAttachmentVO> sysAttachments = sysAttachmentService.listByLinkedId(id);
        productVO.setAttachmentList(sysAttachments);
        return productVO;
    }

    /**
     * 获取产品详情
     * @param productId 产品主键
     * @return 内容
     */
    @IgnoreResponseWrap
    @GetMapping(value = "/getDetail")
    public ResponseResult getDetail(Long productId) {
        Assert.notNull(productId, "参数不能为空");
        return ResponseResult.ok( baseService.getDetailByProductId(productId));
    }

    /**
     * 删除
     * 
     * @param id
     *            产品管理主键
     * @return 删除状态。true标识修改成功，false标识修改失败
     */
    @SysLog("删除产品")
    @GetMapping(value = "/delete")
    @PreAuthorize("hasAuthority('detection:product:delete')")
    public boolean delete(Long id) {
        Assert.notNull(id, "ID不能为空");
        return baseService.delete(id);
    }

    /**
     * 列表
     * 
     * @param searchForm
     *            查询条件
     * @return 产品管理列表集合
     */
    @PostMapping(value = "/list")
    public BasePage<DetectionProductVO> page(@RequestBody DetectionProductSearchForm searchForm) {
        return baseService.page(searchForm);
    }

}